<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
 * Default Kohana controller. This controller should NOT be used in production.
 * It is for demonstration purposes only!
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class User_Controller extends Template_Controller {
	public $template = 'kohana/template';
	public $requestUrl = 'requested_url';
	public function RequestUrl()
	{
		if(isset($_SESSION[$this->requestUrl]))
		{
			return $_SESSION[$this->requestUrl];
		}
		return 'index/index';
	}
	public function login($role="")
	{
		$this->template->title = 'User Login';
		$form = new Forge('user/login');
		$form->input('username')->label(TRUE)->rules('required|length[5,32]');
		$form->password('password')->label(TRUE)->rules('required|length[5,32]');
		$form->submit('submit');
		$this -> template -> content = $form->render();

		if ($form->validate())
		{
			$user = ORM::factory('user', $form->username->value);
			if (Auth::instance()->login($user, $form->password->value))
			{
				url::redirect($this->RequestUrl());
				//$form->password->add_error('login', 'correct username or password.');
			}
			else
			{
				$form->password->add_error('login_failed', 'Invalid username or password.');
			}
		}

	}
	public function Register()
	{
		$this->template->title = 'User Register';
		$form = new Forge(url::current());
		$form->input('username')->label(TRUE)->rules('required|length[5,32]');
		$form->password('password')->label(TRUE)->rules('required|length[5,32]');
		$form->submit('submit');
		$this -> template -> content = $form->render();

		if ($form->validate())
		{

		}
	}
	public function logout()
	{
		// Force a complete logout
		Auth::instance()->logout(TRUE);

		// Redirect back to the login page
		url::redirect('user/login');
	}

}
